/*
 * Copyright (c) 2022-2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef OHOS_LITE
#include "test_player_old.h"

#include <iostream>
#include <thread>
#include <chrono>

#include "i_player_engine.h"
#include "foundation/log.h"

#include "i_engine_factory.h"

extern "C" {
__attribute__((visibility("default"))) OHOS::Media::IEngineFactory* CreateEngineFactory();
}

using namespace OHOS::Media;

namespace OHOS::Media::Test {
bool g_playFinishedOld = false;

class PlayerCallbackImplOld : public IPlayerEngineObs {
public:
    void OnError(PlayerErrorType errorType, int32_t errorCode) override
    {
    }
    void OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody) override
    {
        if (type == INFO_TYPE_EOS) {
            g_playFinishedOld = true;
        }
    }
};

bool StartPlayer(const std::string& url)
{
    std::cout << "Use media_standard interface player." << std::endl;
    g_playFinishedOld = false;
    auto engineFactory = std::unique_ptr<OHOS::Media::IEngineFactory>(CreateEngineFactory());
    auto player = engineFactory->CreatePlayerEngine();
    auto callback = std::make_shared<PlayerCallbackImplOld>();
    player->SetObs(callback);
    FALSE_RETURN_V(0 == player->SetSource(url), false);
    FALSE_RETURN_V(0 == player->SetLooping(false), false);
    FALSE_RETURN_V(0 == player->Prepare(), false);
    FALSE_RETURN_V(0 == player->Play(), false);
    while (!g_playFinishedOld) {
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }
    FALSE_RETURN_V(g_playFinishedOld, false);
    return true;
}
}
#endif